import { Injectable } from '@nestjs/common';
import { CreateOrganizationDto } from './dto/create-organization.dto';
import { UpdateOrganizationDto } from './dto/update-organization.dto';
import { Repository } from 'typeorm';
import { Organization } from './entities/organization.entity';
import { InjectRepository } from '@nestjs/typeorm';

@Injectable()
export class OrganizationService {
  constructor(
    @InjectRepository(Organization)
    private readonly organizationRepository: Repository<Organization>,
  ) {
  }

  async create(createOrganizationDto: CreateOrganizationDto) {
    await this.organizationRepository.save(createOrganizationDto);
  }

  findAll() {
    return this.organizationRepository.find();
  }

  findOne(id: number) {
    return this.organizationRepository.findOne({ where: { id } });
  }

  update(id: number, updateOrganizationDto: UpdateOrganizationDto) {
    return this.organizationRepository.update(id, updateOrganizationDto);
  }

  async remove(id: number) {
    const organization = await this.organizationRepository.findOne({
      where: { id },
    });
    if (organization) {
      return this.organizationRepository.remove(organization);
    } else {
      throw new Error('删除机构失败');
    }
  }
}
